import axios from 'axios'
import i18n from '@/i18n/i18n'
import moment from 'moment'
import { toCamelCase } from '@/utils'
import { isArray, isObject } from '@/utils/types'
import messages from './langs'

/**
 * @description: 设置相关库的语言
 * @param {*} lang
 * @return {*}
 */
export function setI18nLanguage(lang) {
  i18n.locale = lang
  moment.locale(lang || 'zh-CN')
  axios.defaults.headers.common['Accept-Language'] = lang || 'zh-CN'
  // document.querySelector('html').setAttribute('lang', lang)
  return lang
}

/**
 * @description: 获取全部语言包，主要是拼装el
 * @param {*} lang
 * @param {*} languagePack
 * @return {*}
 */
export function getFullLanguage(lang, languagePack) {
  switch (lang) {
    case 'en':
      return { ...require('element-ui/lib/locale/lang/en').default, ...languagePack }
    case 'zh-CN':
      return { ...require('element-ui/lib/locale/lang/zh-CN').default, ...languagePack }
    case 'zh-TW':
      return { ...require('element-ui/lib/locale/lang/zh-TW').default, ...languagePack }
    case 'ko':
      return { ...require('element-ui/lib/locale/lang/ko').default, ...languagePack }
    case 'de':
      return { ...require('element-ui/lib/locale/lang/de').default, ...languagePack }
    case 'fr':
      return { ...require('element-ui/lib/locale/lang/fr').default, ...languagePack }
    case 'ru-RU':
      return { ...require('element-ui/lib/locale/lang/ru-RU').default, ...languagePack }
    case 'es':
      return { ...require('element-ui/lib/locale/lang/es').default, ...languagePack }
    case 'ja':
      return { ...require('element-ui/lib/locale/lang/ja').default, ...languagePack }
    case 'ar':
      return { ...require('element-ui/lib/locale/lang/ar').default, ...languagePack }
    case 'pt':
      return { ...require('element-ui/lib/locale/lang/pt').default, ...languagePack }
    case 'da':
      return { ...require('element-ui/lib/locale/lang/da').default, ...languagePack }
    case 'it':
      return { ...require('element-ui/lib/locale/lang/it').default, ...languagePack }
    case 'nl':
      return { ...require('element-ui/lib/locale/lang/nl').default, ...languagePack }
    case 'vi':
      return { ...require('element-ui/lib/locale/lang/vi').default, ...languagePack }
    case 'tr-TR':
      return { ...require('element-ui/lib/locale/lang/tr-TR').default, ...languagePack }
    case 'fa':
      return { ...require('element-ui/lib/locale/lang/fa').default, ...languagePack }
    case 'th':
      return { ...require('element-ui/lib/locale/lang/th').default, ...languagePack }
    case 'id':
      return { ...require('element-ui/lib/locale/lang/id').default, ...languagePack }
    case 'pl':
      return { ...require('element-ui/lib/locale/lang/pl').default, ...languagePack }
  }
  return languagePack
}

/**
 * @description: 载入语言包
 * @param {*} lang
 * @param {*} languagePack
 * @return {*}
 */
export function loadLanguageSync(lang, languagePack) {
  let langMessages = getFullLanguage(lang, languagePack)
  if (lang === 'zh-CN') {
    langMessages = {
      ...messages['zh-CN'],
      ...langMessages
    } // 保证本地自带的语言文件，一定包含
  }
  i18n.setLocaleMessage(lang, langMessages)
  return setI18nLanguage(lang)
}

/**
* @description: 设置langkey
* @param {*} childItem  当前自定义字段数据
* @param {*} crmType 模块类型
* @param {*} module 使用场景的类型
* @param {*} label  选项的文本
* @return {*}
*/
export function setLangKey(childItem, crmType, module, label) {
  // 错误信息多语言处理
  if (module == 'error') {
    return `error.${childItem.code}`
  }
}
export function getLangLabel(langKey, label) {
  if (i18n.locale == 'zh-CN') {
    return label
  } else {
    if (langKey && langKey.indexOf('_') !== -1) {
      langKey = toCamelCase(langKey)
    }
    if (langKey && langKey.indexOf('Id') !== -1) {
      langKey = langKey.replace('Id', 'Name')
    }
    if (langKey && window.app.$i18n.te(langKey)) {
      return window.app.$i18n.t(langKey)
    } else {
      return label
    }
  }
}
// 处理操作记录类型的数据 返回key值用{}
export function getLangContent(str) {
  var arr = str.match(/\{[^\}]+\}/g)
  if (!arr) return str
  for (var i = 0; i < arr.length; i++) {
    const key = arr[i].slice(1, (arr[i].length - 1))
    str = str.replace(arr[i], ' ' + window.app.$i18n.t(key) + ' ')
  }
  return str
}
// 处理后端返回多语言key值适配的方法
export function getKey(data) {
  // 跟进记录类型\淘汰原因
  if (data && data.data &&
    (data.data.hasOwnProperty('followRecordOptions') ||
    data.data.hasOwnProperty('recruit'))) {
    const langKeyMap = data.data.languageKeyMap
    const getKeyNameArr = []
    if (langKeyMap) {
      for (var key in langKeyMap) {
        if (window.app.$i18n.te(langKeyMap[key])) {
          getKeyNameArr.push(window.app.$i18n.t(langKeyMap[key]))
        } else {
          if (data.data.hasOwnProperty('followRecordOptions')) {
            getKeyNameArr.push(data.data.followRecordOptions[key])
          } else if (data.data.hasOwnProperty('recruit')) {
            getKeyNameArr.push(data.data.recruit[key])
          }
        }
      }
    }
    data.data = getKeyNameArr
    return
  }
  if (isArray(data)) {
    // 是数组遍历数组
    data.forEach((item) => {
      getKey(item)
    })
  }
  if (isObject(data)) {
    // 是对象找 languageKeyMap 属性适配key
    if (data.hasOwnProperty('languageKeyMap')) {
      if (isObject(data.languageKeyMap)) {
        Object.keys(data.languageKeyMap).forEach((item) => {
          // 处理下拉选
          if (data.setting && data.setting.length !== 0 && data.formType != 'serial_number') {
            const newSetting = []
            data.setting.forEach((item, index) => {
              // 有name属性的setting
              if (item.name) {
                if (window.app.$i18n.te(data.languageKeyMap[index])) {
                  item.name = window.app.$i18n.t(data.languageKeyMap[index])
                  newSetting.push(item)
                } else {
                  newSetting.push(item)
                }
              } else {
                // 纯文本的setting
                if (window.app.$i18n.te(data.languageKeyMap[index])) {
                  newSetting.push(window.app.$i18n.t(data.languageKeyMap[index]))
                } else {
                  newSetting.push(item)
                }
              }
            })
            data.setting = newSetting
          }

          const keyName = item.split('_')[0]
          if (data.hasOwnProperty(keyName)) {
            data[`_${keyName}`] = data[keyName]
            if (window.app.$i18n.te(data.languageKeyMap[item])) {
              const reg = /[\u4e00-\u9fa5]/
              // 客户满意度 key值为中文 单独处理
              if (!reg.test(keyName)) {
                // 产品分类 label 和 name是相同值时
                const copyName = data[keyName]
                const langValue = window.app.$i18n.t(data.languageKeyMap[item])
                if (copyName == data.label) {
                  data.label = langValue
                }
                data[keyName] = langValue
              }
            }
          }
        })
      }
    }
    // 往下层继续查找
    Object.keys(data).forEach((item) => {
      getKey(data[item])
    })
  }
}
// 处理客户满意度
export function getSatisfactionLang(fieldList, keyMap) {
  fieldList.forEach((item1) => {
    Object.keys(keyMap).forEach((key) => {
      const keyName = key.split('_')[0]
      if (item1.name == keyName) {
        item1.name = window.app.$i18n.te(keyMap[key]) ? window.app.$i18n.t(keyMap[key]) : item1.name
      }
    })
  })
  return fieldList
}
